package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.constant.SecurityConst;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.utils.PinyinUtils;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.SysRole;
import com.wwu.system.mapper.SysRoleMapper;
import com.wwu.system.service.ISysRoleService;
import com.wwu.system.service.ISysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

/**
 *  系统角色服务实现类
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Resource
    private ISysUserRoleService sysUserRoleService;

    /**
     * 根据角色名获取角色
     * @param roleName   角色名
     * @return com.wwu.system.entity.SysRole
     * @author 一蓑烟雨
     * @date 2023/6/12 11:20
     */
    @Override
    public SysRole getRoleByRoleName(String roleName) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getRoleName ,roleName)
                .eq(SysRole::getRoleStatus,0);

        return this.getOne(wrapper);
    }

    /**
     * 分页查询角色信息
     * @param pageNum  页码
     * @param pageSize  每页数量
     * @param sysRole sysRole实体对象
     * @return com.wwu.common.web.ResultPage<com.wwu.system.entity.SysRole>
     * @author 一蓑烟雨
     * @date 2023/6/12 11:20
     */
    @Override
    public ResultPage<SysRole> getRoleListPage(Integer pageNum, Integer pageSize, SysRole sysRole) {
        Page<SysRole> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(sysRole.getRoleName())){
            wrapper.like(SysRole::getRoleName,sysRole.getRoleName());
        }
        if(null != sysRole.getRoleStatus()){
            wrapper.eq(SysRole::getRoleStatus,sysRole.getRoleStatus());
        }
        IPage<SysRole> sysRolePage = this.page(page, wrapper);
        return new ResultPage<>(sysRolePage);
    }

    /**
     * 添加角色信息
     * @param sysRole 角色信息
     * @author 一蓑烟雨
     * @date 2023/6/13 21:57
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveRole(SysRole sysRole) {
        AssertUtil.isTrue(!StringUtils.hasText(sysRole.getRoleName()),"角色名不能为空");
        //角色必须以ROLE_开头
        if(!sysRole.getRoleName().startsWith(SecurityConst.ROLE_PREFIX)){
            sysRole.setRoleName(SecurityConst.ROLE_PREFIX+sysRole.getRoleName());
        }
        AssertUtil.isTrue(null != this.getRoleByRoleName(sysRole.getRoleName()),"角色名已存在");

        sysRole.setPy(PinyinUtils.getPinYin(sysRole.getRoleName()));
        sysRole.setWb(PinyinUtils.getWuBi(sysRole.getRoleName()));
        AssertUtil.isTrue(!this.save(sysRole),"角色信息添加失败！");
    }

    /**
     * 更新角色信息
     * @param sysRole 角色信息
     * @author 一蓑烟雨
     * @date 2023/6/2 17:20
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateRole(SysRole sysRole) {
        AssertUtil.isTrue(!StringUtils.hasText(sysRole.getRoleName()),"角色名不能为空");
        //角色必须以ROLE_开头
        if(!sysRole.getRoleName().startsWith(SecurityConst.ROLE_PREFIX)){
            sysRole.setRoleName(SecurityConst.ROLE_PREFIX+sysRole.getRoleName());
        }
        SysRole currRole = this.getRoleByRoleName(sysRole.getRoleName());
        AssertUtil.isTrue(null != currRole && !(currRole.getRoleId().equals(sysRole.getRoleId())),"角色名已存在");

        sysRole.setPy(PinyinUtils.getPinYin(sysRole.getRoleName()));
        sysRole.setWb(PinyinUtils.getWuBi(sysRole.getRoleName()));
        AssertUtil.isTrue(!this.updateById(sysRole),"角色信息更新失败");
    }

    /**
     *  删除角色信息(物理删除)
     * @param roleIds  角色id
     * @author 一蓑烟雨
     * @date 2023/6/12 21:44
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteRole(List<String> roleIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(roleIds),"请选择删除的记录！");
        //删除用户角色信息
        AssertUtil.isTrue(!sysUserRoleService.deleteUserRoleByRoleId(roleIds),"用户角色信息删除失败！");
        //删除角色信息
        AssertUtil.isTrue(!this.removeByIds(roleIds),"角色信息删除失败！");
    }
}
